﻿using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Map_Creator.MapInfo;

namespace Map_Creator.MainForm.Controls
{
    public partial class MapTreeView : TreeView 
    {
        private Font BoldFont;

        public static MapTreeView Instance;

        public MapTreeView()
        {
            InitializeComponent();

            Initialize();
        }

        private void Initialize()
        {
            Instance = this;
            BoldFont = new Font(Font.FontFamily, Font.SizeInPoints, FontStyle.Bold);
            MapInfo.Map.SelectedLayerChanged += Map_SelectedLayerChanged;
            MapInfo.Map.SelectedDataGridChanged += Map_SelectedDataGridChanged;
            MapInfo.Map.SelectedObjectLayerChanged += Map_SelectedObjectLayerChanged;
            TileSet.PrimaryTileSetChanged += TileSet_PrimaryTileSetChanged;
            TileSet.SelectedDataGridChanged += TileSet_SelectedDataGridChanged;
        }

        void Map_SelectedObjectLayerChanged(MapInfo.Map map)
        {
            BeginUpdate();
            foreach (ObjectLayer objectLayers in map.ObjectLayers)
            {
                objectLayers.Node.NodeFont = Font;
            }

            if (map.CurrentObjectLayer != null)
                map.CurrentObjectLayer.Node.NodeFont = BoldFont;

            EndUpdate();
        }

        void TileSet_SelectedDataGridChanged(MapInfo.Map map)
        {
            BeginUpdate();
            foreach (CustomDataGrid dataGrid in map.CurrentTileSet.DataGrids)
            {
                dataGrid.Node.NodeFont = Font;
            }

            if (map.CurrentTileSet.CurrentDataGrid != null)
                map.CurrentTileSet.CurrentDataGrid.Node.NodeFont = BoldFont;

            EndUpdate();
        }

        void TileSet_PrimaryTileSetChanged(MapInfo.Map map)
        {
            if (map == null) return;

            BeginUpdate();

            foreach (TileSet tileSet in map.TileSets)
            {
                tileSet.Node.NodeFont = Font;
            }

            if (map.CurrentTileSet != null)
                map.CurrentTileSet.Node.NodeFont = BoldFont;

            EndUpdate();
        }

        void Map_SelectedDataGridChanged(MapInfo.Map map)
        {
            BeginUpdate();
            foreach (CustomDataGrid dataGrid in map.DataGrids)
            {
                dataGrid.Node.NodeFont = Font;
            }

            if (map.CurrentDataGrid != null)
                map.CurrentDataGrid.Node.NodeFont = BoldFont;

            EndUpdate();
        }

        void Map_SelectedLayerChanged(MapInfo.Map map)
        {
            BeginUpdate();

            foreach (Layer layer in map.Layers)
            {
                layer.Node.NodeFont = Font;
            }

            if (map.CurrentLayer != null)
                map.CurrentLayer.Node.NodeFont = BoldFont;

            EndUpdate();
        }

        public MapTreeView(IContainer container)
        {
            container.Add(this);

            InitializeComponent();

            Initialize();
        }

        private void OnNodeClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            e.Node.TreeView.SelectedNode = e.Node;

            var node = e.Node as ExtendedTreeNode;
            if (node == null) return;

            switch (node.Type)
            {
                case NodeType.Map:
                    {
                        var map = node.Obj as MapInfo.Map;

                        MapInfo.Map.SelectedMap = map;
                    }
                    break;
            }
        }

        private void OnNodeDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            var node = e.Node as ExtendedTreeNode;

            if (node == null) return;

            switch (node.Type)
            {
                case NodeType.Layer:
                    {
                        var layer = node.Obj as Layer;

                        var map = layer.AssociatedMap;

                        MapInfo.Map.SelectedMap = map;

                        map.CurrentLayer = layer;
                    }
                    break;
                case NodeType.TileSet:
                    {
                        var tileSet = node.Obj as TileSet;

                        var map = tileSet.AssociatedMap;

                        MapInfo.Map.SelectedMap = map;

                        map.CurrentTileSet = tileSet;
                    }
                    break;
                case NodeType.DataGrid:
                    {
                        var dataGrid = node.Obj as CustomDataGrid;

                        var map = dataGrid.AssociatedMap;

                        MapInfo.Map.SelectedMap = map;

                        if (dataGrid.AssociatedTileSet == null)
                        {
                            map.CurrentDataGrid = dataGrid;
                        }
                        else
                        {
                            map.CurrentTileSet = dataGrid.AssociatedTileSet;
                            map.CurrentTileSet.CurrentDataGrid = dataGrid;
                        }
                    }
                    break;
                case NodeType.ObjectLayer:
                    {
                        var objectLayer = node.Obj as ObjectLayer;

                        var map = objectLayer.AssociatedMap;

                        MapInfo.Map.SelectedMap = map;
                        map.CurrentObjectLayer = objectLayer;
                    }
                    break;
            }
        }

        private void OnKeyDown(object sender, KeyEventArgs e)
        {
            var node = (sender as TreeView).SelectedNode;
            if (node == null) return;
            if (node.ContextMenuStrip == null) return;

            var key = e.KeyCode;
            
            foreach (ToolStripMenuItem item in node.ContextMenuStrip.Items)
            {
                if (item.ShortcutKeys == key)
                    item.PerformClick();
            }
        }
    }
}
